"""
文件处理相关的工具模块
"""


def print_file_info(file_name):
    """
    打印文件内的内容，文件不存在就提示
    :param file_name: 要访问的文件的路径
    :return: None
    """
    f = None
    try:
        f = open(file_name, "r", encoding="UTF-8")
        content = f.read()
        print("文件内容如下：")
        print(content)
    except Exception as e:
        print(f"程序出现错误，原因是{e}")
    finally:
        if f:
            f.close()


def append_to_file(file_name,data):
    """
    功能是追加内容进一个文件
    :param file_name: 要被追加内容的文件的路径
    :param data: 要追加的内容
    :return: None
    """
    with open(file_name, "a", encoding="UTF-8") as f:
        f.write(data)
        print()
        f.close()


if __name__ == "__main__":
    print_file_info(r"C:\Users\Lenovo\OneDrive\桌面\test1.txt")
    append_to_file(r"C:\Users\Lenovo\OneDrive\桌面\test1.txt", "天天开心")
